package developer.算法.链表.两数相加;

/**
 * @author zhangyongkang
 * @time 2025/3/20 15:25
 * @description 给你两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的，并且每个节点只能存储 一位 数字。
 * <p>
 * 请你将两个数相加，并以相同形式返回一个表示和的链表。
 * <p>
 * 你可以假设除了数字 0 之外，这两个数都不会以 0 开头。
 * <p>
 * l1 = [2,4,3], l2 = [5,6,4]
 * 输出：[7,0,8]
 * 解释：342 + 465 = 807.
 * <p>
 * l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
 * 9 9 9 9
 * 8 9 9 9 0
 * 输出：[8,9,9,9,0,0,0,1]
 */
public class LiangShuXiangJia {

    public static void main(String[] args) {
//        ListNode listNode = addTwoNumbers(
//                new ListNode(2, new ListNode(4, new ListNode(3))),
//                new ListNode(5, new ListNode(6, new ListNode()))
//        );
        ListNode listNode = addTwoNumbers(
                new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9))))))),
                new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9))))
        );
        System.out.println(listNode);
    }


    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int preVal = 0;
        ListNode head = new ListNode();
        ListNode current = head;
        while (l1 != null || l2 != null) {
            if (l1 != null && l2 != null) {
                int plusVal = l1.val + l2.val + preVal;
                if (plusVal < 10) {
                    current.next = new ListNode(plusVal);
                    preVal = 0;
                } else {
                    preVal = plusVal / 10;
                    current.next = new ListNode(plusVal % 10);
                }
                l1 = l1.next;
                l2 = l2.next;
                current = current.next;
                continue;
            }
            if (l1 != null && l2 == null) {
                int plusVal = l1.val + preVal;
                if (plusVal < 10) {
                    current.next = new ListNode(plusVal);
                    preVal = 0;
                } else {
                    preVal = plusVal / 10;
                    current.next = new ListNode(plusVal % 10);
                }
                l1 = l1.next;
                current = current.next;
                continue;
            }

            if (l1 == null && l2 != null) {
                int plusVal = l2.val + preVal;
                if (plusVal < 10) {
                    current.next = new ListNode(plusVal);
                    preVal = 0;
                } else {
                    preVal = plusVal / 10;
                    current.next = new ListNode(plusVal % 10);
                }
                l2 = l2.next;
                current = current.next;
                continue;
            }

        }
        if (preVal > 0) {
            current.next = new ListNode(preVal);
        }
        return head.next;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
